package com.yanqu.road.server.manager.activity.fivehero;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFiveHeroMgr extends TempCrossMgr {


    // 预留参数
    public static Map<Integer, String> hotUpdateMap = new ConcurrentHashMap<>();

    private static Map<Integer, CrossFiveHeroActivityGroup> activityGroupMap = new ConcurrentHashMap<>();

    public static void timerTask() {
        List<CrossFiveHeroActivityGroup> acList = new ArrayList<>(activityGroupMap.values());
        for(CrossFiveHeroActivityGroup ac : acList){
            ac.timerTask();
        }
    }

    @Override
    public boolean reloadData() throws Exception {
        synchronized (this) {
            if (activityGroupMap == null) {
                activityGroupMap = new ConcurrentHashMap<>();
            }
            // 获取活动
            String nowTimeStr = DateHelper.getCurrentDateTimeString();
            List<Integer> activityTypeList = new ArrayList<>();
            activityTypeList.add(eActivityType.FiveHero.getValue());
            Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
            // 重新加载数据
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                if (!activityGroupMap.containsKey(activityInfo.getActivityId())) {
                    Map<Integer, UnionActivityGroup> groupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
                    if (groupMap != null) {
                        for (int groupId : groupMap.keySet()){
                            getFiveHeroActivity(activityInfo.getActivityId(),groupId);
                        }
                    }
                }
            }
        }
        return true;
    }

    public static synchronized CrossFiveHeroActivity getFiveHeroActivity(int activityId,int groupId) {
        // 拿分组配置
        UnionActivityGroup groupData = Cross2UnionActivityGroupMgr.getGroupData(activityId, groupId);
        if (groupData == null) {
            return null;
        }
        if (activityGroupMap.get(activityId) == null) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return null;
            }
            CrossFiveHeroActivityGroup activity = new CrossFiveHeroActivityGroup(activityInfo);
            activityGroupMap.put(activityId, activity);
        }
        CrossFiveHeroActivityGroup group = activityGroupMap.get(activityId);

        return group.getActivity(groupId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        if (activityGroupMap != null) {
            for (CrossFiveHeroActivityGroup activityGroup : activityGroupMap.values()) {
                activityGroup.save();
            }
        }
        return true;
    }

    public static void reloadConfig(int activityId) {
        if (activityGroupMap.containsKey(activityId)) {
            activityGroupMap.get(activityId).reloadConfig();
        }
    }

    public static void repair(int activityId) {
        if (activityGroupMap.containsKey(activityId)) {
            activityGroupMap.get(activityId).repair();
        }
    }

    public boolean removeExpireData(){
        List<Integer> acList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for(Integer activityId : activityGroupMap.keySet()){
            CrossFiveHeroActivityGroup data = activityGroupMap.get(activityId);
            if(data.getActivityInfo() == null || (nowTime > data.getActivityInfo().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                acList.add(activityId);
            }
        }
        for(Integer activityId : acList){
            activityGroupMap.remove(activityId);
        }
        return true;
    }
}
